/**
 * Name        : BadaTorch
 * Version     : 
 * Vendor      : 
 * Description : 
 */

#include "BadaTorch.h"
#include "MainForm.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::System;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

BadaTorch::BadaTorch()
{
}

BadaTorch::~BadaTorch()
{
}

Application*
BadaTorch::CreateInstance(void)
{
	// Create the instance through the constructor.
	return new BadaTorch();
}

bool BadaTorch::OnAppInitializing(AppRegistry& appRegistry)
{
	// TODO:
	// Initialize UI resources and application specific data.
	// The application's permanent data and context can be obtained from the appRegistry.
	//
	// If this method is successful, return true; otherwise, return false.
	// If this method returns false, the application will be terminated.

	// Uncomment the following statement to listen to the screen on/off events.
	//PowerManager::SetScreenEventListener(*this);

	// Create a form
	this->pMainForm = new MainForm();
	pMainForm->Initialize();

	// Add the form to the frame
	Frame *pFrame = GetAppFrame()->GetFrame();
	pFrame->AddControl(*pMainForm);

	// Set the current form
	pFrame->SetCurrentForm(*pMainForm);

	// Draw and Show the form
	pMainForm->Draw();
	pMainForm->Show();

	return true;
}

bool BadaTorch::OnAppTerminating(AppRegistry& appRegistry,
		bool forcedTermination)
{
	// TODO:
	// Deallocate resources allocated by this application for termination.
	// The application's permanent data and context can be saved via appRegistry.
	return true;
}

void BadaTorch::OnForeground(void)
{
	// TODO:

	// save the	camera state (recordnig or not).
	AppLog("OnForeground");

	if (pMainForm->__pButton->GetText() == pMainForm->GetString("IDS_ON"))
	{
		if (pMainForm->__pVideoRecorder->GetState()
				!= Osp::Media::RECORDER_STATE_RECORDING
				&& pMainForm->firstRunCheckOK == true)
		{
			pMainForm->firstRunCheck();
			// Make sure it continues to light.
			AppLog("start flash light onForeground");
			if (pMainForm->startRecorder() != E_SUCCESS)
			{
				pMainForm->showMessageBox(pMainForm->GetString(
						L"IDS_DEVICE_BUSY"), false);
			}
			else
			{
				pMainForm->__pButton->SetTextColor(pMainForm->__offColor);
				pMainForm->__pButton->SetText(pMainForm->GetString(L"IDS_ON"));
			}
		}
	}


}

void BadaTorch::OnBackground(void)
{
	// TODO:
	// Restore the camera state
	// Set the button Text according to the camera state.
	AppLog("OnBackground");
}

void BadaTorch::OnLowMemory(void)
{
	// TODO:
	// Free unused resources or close the application.
}

void BadaTorch::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
	// TODO:
	// Handle any changes in battery level here.
	// Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.

}

void BadaTorch::OnScreenOn(void)
{
	// TODO:
	// Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
}

void BadaTorch::OnScreenOff(void)
{
	// TODO:
	//  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.
	// Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.
	// Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
}

void BadaTorch::OnBatteryLevelChangedInPercentage(int percentage)
{

//	Label *label = static_cast<Label *> (pMainForm->GetControl(L"IDC_TO"));
//	int level;
//	Battery::GetCurrentLevelInPercentage(level);
//	String s;
//	s.Append(level);
//	s.Append(L"%");
//	label->SetText(s);
//	AppLogDebug("batter level %d", level);
}

